@extends('manage.layout')

@section('content')
    <section class="content-header">
        <h1>
            添加案例
            <small>Preview</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Forms</a></li>
            <li class="active">General Elements</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">内容</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="{{ url('update') }}" method="post" role="form" style="max-width: 800px;" enctype="multipart/form-data" id="edit_form">
                        {{csrf_field()}}
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="id" value="{{ $case->id }}">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="exampleInputPassword1">案例编号</label>
                                <input type="text" name="number" class="form-control" value="{{ $case->number }}" placeholder="Case Name" disabled>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">案例名称</label>
                                <input type="text" name="name" class="form-control" value="{{ $case->name }}" placeholder="Case Name">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">权重</label>
                                <input type="number" name="rank" class="form-control" placeholder="rank" value="{{ $case->rank }}">
                            </div>

                            <div class="form-group">
                                <label for="exampleInputPassword1">所属行业</label>
                                <select name="industry" class="form-control">
                                    <option value="{{ $case->industry }}">{{ APP\Model\CaseIndustry::find($case->industry)['name'] }}</option>
                                    @foreach($ind as $i)
                                        <option value="{{ $i['id'] }}">{{ $i['name'] }}</option>
                                    @endforeach
                                </select>
                                {{--<input type="text" name="industry" class="form-control" id="exampleInputPassword1" placeholder="Category Name">--}}
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">应用场景</label>
                                <select name="scene" class="form-control">
                                    @if($case->scene === 0)
                                        <option value="0" selected>线上</option>
                                        <option value="1">线下</option>
                                    @elseif($case->scene === 1)
                                        <option value="0" >线上</option>
                                        <option value="1" selected>线下</option>
                                    @endif
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">标签</label>
                                <select name="lable" class="form-control">
                                    @if($case->lable == 1)
                                        <option value="{{ $case->lable }}">精品</option>
                                    @else
                                        <option value="{{ $case->lable }}">普通</option>
                                    @endif
                                    <option value="0">普通</option>
                                    <option value="1">精品</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">所属类别</label>
                                <select name="grade" id="grade" class="form-control" onchange="change()">
                                    <option value="{{ $cate_son->parent_id }}">{{ App\Model\ProductCategory::find($cate_son->parent_id)['name'] }}</option>
                                    @foreach($category as $c)
                                        <option value="{{ $c['id'] }}">{{ $c['name'] }}</option>
                                    @endforeach
                                </select>
                                <select name="classes" id="classes" class="form-control">
                                    <option value="{{ $cate_son->id }}">{{ $cate_son->name }}</option>
                                </select>
                                {{--<input type="text" name="category" class="form-control" id="exampleInputPassword1" placeholder="Category">--}}
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">所属产品</label>
                                <input type="text" name="product_id" class="form-control" value="{{ $case->product_id }}" placeholder="Product">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">价格</label>
                                <input type="text" name="price" class="form-control" value="{{ $case->price }}" placeholder="Price">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">制作单位</label>
                                <input type="text" name="producer" class="form-control" value="{{ $case->producer }}" placeholder="Make Company">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">制作时间</label>
                                <input type="text" name="make_time" class="form-control" value="{{ $case->make_time }}" placeholder="Make Time">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">在线链接</label>
                                <input type="text" name="url" class="form-control" value="{{ $case->location }}" placeholder="URL">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">案例简介</label>
                                {{--<input type="text" name="url" class="form-control" id="exampleInputPassword1" placeholder="URL">--}}
                                <textarea name="brief" class="form-control"  placeholder="Brief">{{ $case->brief }}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">案例内容</label>
                                {{--<input type="text" name="url" class="form-control" id="exampleInputPassword1" placeholder="URL">--}}
                                <textarea id="editor_id" name="content" class="form-control"  placeholder="Content">{{ $case->content }}</textarea>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
                <div class="box box-success">
                    <div class="box-header with-border">
                        <h3 class="box-title">上传图片</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="{{ url('picture') }}" method="post" role="form" style="max-width: 800px;" enctype="multipart/form-data" id="picture_form">
                        {{csrf_field()}}
                        <input type="hidden" name="case_id" value="{{ $case->id }}">
                        <div class="box-body">
                            <h4>您所选择的文件列表：</h4>
                            <div id="ossfile">你的浏览器不支持flash,Silverlight或者HTML5！</div>

                            <br/>

                            <div id="container">
                                <a id="selectfiles" href="javascript:void(0);" class='btn btn-primary btn-sm'>选择文件</a>
                                <a id="postfiles" href="javascript:void(0);" class='btn btn-primary btn-sm'>开始上传</a>
                            </div>
                        </div>
                        <div class="box-footer">
                            <button onclick="add()" class="btn btn-primary">增加一张图片</button>
                        </div>
                    </form>

                </div>
                <div class="box box-warning">
                    <div class="box-header with-border">
                        <h3 class="box-title">上传附件</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                </div>
            </div>
        </div>
    </section>
@stop
@section('script')
    <script>
        //Date picker
        $('#datepicker').datepicker({
            autoclose: true
        });

        //类别二级联动
        function change() {
            var id = $("select[name='grade']").val();
            var data = {id:id};
            $.ajax({
                url: "/classes/"+id,
                type: "get",
                data: data,
                dataType: "json",
                success: function(data) {
                    console.log(data);
                    $("#classes").empty();
                    var count = data.length;

                    var i = 0;
                    var b="";
                    for(i=0;i<count;i++){
                        b+="<option value='"+data[i].id+"'>"+data[i].name+"</option>";
                    }
                    $("#classes").append(b);
                }
            });
        }

        //上传案例
        $(function() {
            //文本编辑器
            var editor;
            KindEditor.ready(function(K) {
                editor = K.create('textarea[name="content"]', {
                    allowFileManager : true
                });
            });
            $("#edit_form").submit(function(event) {
                event.preventDefault();
                var url = $(this).attr("action");
                var data = $("#edit_form").serialize();
                console.log(data);
                $.ajax({
                    url: url,
                    type: "POST",
                    data: data,
                    dataType: "json",
                    success: function(res) {
                        console.log(res);
                        if(res.code == 100) {
                            window.location.href = "/caseList";
                            layer.msg(res.msg)
                        }
                        else {
                            layer.msg(res.msg)
                        }
                    }
                });
            });
        });

        //案例图片上传
        $(function() {
            $("#picture_form").submit(function(event) {
                event.preventDefault();
                var url = $(this).attr("action");
                var data = $("#picture_form").serialize();
                console.log(data);
                $.ajax({
                    url: url,
                    type: "POST",
                    data: data,
                    dataType: "json",
                    success: function(res) {
                        console.log(res);
                        if(res.code == 100) {
                            window.location.href = "/caseList";
                            layer.msg(res.msg)
                        }
                        else {
                            layer.msg(res.msg)
                        }
                    }
                });
            });
        });
    </script>

@stop
